package com.boss.asset.dao.impl;

import com.boss.asset.dao.IAssetInfoFlowDao;
import com.boss.asset.entity.BasAssetBiz;
import com.boss.common.enums.IsDeletedEnum;
import com.boss.common.enums.IsEnabledEnum;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AssetInfoFlowDaoImpl extends BaseDaoImpl implements IAssetInfoFlowDao {

    private final static String TABLE_NAME = "ASSET_INFO_FLOW";

    @Override
    public void insert(BasAssetBiz data) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO ").append(TABLE_NAME);
        sql.append("(ID,OFFICE_HOUSE_AREA,BISNISS_HOUSE_AREA,OTHER_HOUSE_AREA,LOC," )
            .append("PRO_STA_DATE,PRO_CONS_STA,OWNERSHIP_CERT,LAND_CERTIFI_DATE,LAND_NO,")
           .append("LAND_OWNERSHIP_AREA,STRUCTURE_AREA,USE_AREA,VEHICLE_TRAV_LICENCE_INFO,VEHICLE_OWNER,")
           .append("VEHICLE_REGIS_DATE, VEHICLE_IDENTI_CODE,VEHICLE_LICENCE_NO,VEHICLES_NUM,HOUSE_OWNERSHIP_CERT,")
           .append("HOUSE_OWNER,HOUSE_OWNERSHIP_NO,HOUSE_OWNERSHIP_AREA,FLOOR_AREA,FI_LEADER," )
           .append("SERVICE_LIFE, DEPRECIATION_METHOD,POSTER_DATE ,ACCOUNTING_STATUS,VOUCHER_NO,IS_SHARED_PLATFORM,") //31
           .append("IS_SHARING,VEHICLE_TYPE ,VEHICLE_NUMB ,VEHICLE_SPE ,VEHICLE_LAW ,")
           .append("VEHICLE_MOD ,INVESTED_AGENCY_INFO ,INVESTED_AGENCY_STOCK_NO ,IS_INVESTED_AGENCY_IN_MARKET ,INVESTED_AGENCY_ORG_FORM_CODE ,STOCK_PERC ,")
           .append("FINAL_INVEST_AMT ,CUR_INVEST_AMT ,PAR_VAL_AMT,TOTAL_INVES ,ACC_INVES ," )
           .append("PRES_YEAR_INVES ,STA_USE_DATE ,NO_REG_ASSET_REASON ,ACQU_DATE ,INVESTED_AGENCY_CODE ,")
           .append("INVESTED_PRO_NAME ,OFFICAL_VEH ,ASSET_ID,BIZ_KEY,ASSET_CODE,")
           .append("FIXED_ASSET_TYPE_CODE,AGENCY_CODE,AGENCY_ID,MOF_DIV_CODE,FIXED_ASSET_STATE_CODE,OWNERSHIP_NATURE_CODE,")//31
           .append("OWNERSHIP_FORM_CODE,DISPOSAL_FORM_CODE,ASSET_AUTH_NO,NUM,NUM_UNIT," )
           .append("PUR_APPROV_UNIT,FIXED_ASS_ACQ_CODE,DIPOS_CHANNEL_CODE,USE_RESPON_SUBJECT_CODE,ASSET_AUTH_SITU_CODE,")
           .append("APPROV_STA_CODE,INIT_ASSET_VAL,ACC_DEP, NET_VAL,FUND_SOURCE ," )
           .append("PUR_ORG_FORM,DIPOS_EARN,EARN_SITU,ASSET_NAME,START_DATE,")
           .append("END_DATE,IS_ENABLED, VALUE_TYPE_CODE,UPDATE_TIME,IS_DELETED, CREATE_TIME , PRO_CODE,IS_FISCAL,AGENCY_NAME)");
            sql.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,")
            .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,")
            .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?)");

        Object[] params = new Object[]{
            data.getId(), data.getOfficeHouseArea(), data.getBisnissHouseArea(), data.getOtherHouseArea(),data.getLoc(),
            data.getProStaDate(), data.getProConsSta(), data.getOwnershipCert(), data.getLandCertifiDate(), data.getLandNo(),
            data.getLandOwnershipArea(), data.getStructureArea(), data.getUseArea(), data.getVehicleTravLicenceInfo(),data.getVehicleOwner(),
            data.getVehicleRegisDate(), data.getVehicleIdentiCode(), data.getVehicleLicenceNo(),data.getVehiclesNum(),data.getHouseOwnershipCert(),
            data.getHouseOwner(), data.getHouseOwnershipNo(),data.getHouseOwnershipArea(),data.getFloorArea(), data.getFiLeader(),
            data.getServiceLife(), data.getDepreciationMethod(),data.getPosterDate(),data.getAccountingStatus(),data.getVoucherNo(), data.getIsSharedPlatform(),
            data.getIsSharing(),data.getVehicleType(),data.getVehicleNumb(),data.getVehicleSpe(), data.getVehicleLaw(),
            data.getVehicleMod(),data.getInvestedAgencyInfo(),data.getInvestedAgencyStockNo(),data.getIsInvestedAgencyInMarket(),data.getInvestedAgencyOrgFormCode(), data.getStockPerc(),
            data.getFinalInvestAmt(),data.getCurInvestAmt(), data.getParValAmt(),data.getTotalInves(), data.getAccInves(),
            data.getPresYearInves(),data.getStaUseDate(),data.getNoRegAssetReason(),data.getAcquDate(),data.getInvestedAgencyCode(),
            data.getInvestedProName(), data.getOfficalVeh(),data.getAssetId(), data.getBizKey(), data.getAssetCode(),
            data.getFixedAssetTypeCode(),data.getAgencyCode(),data.getAgencyId(),data.getMofDivCode(),data.getFixedAssetStateCode(),data.getOwnershipNatureCode(),
            data.getOwnershipFormCode(),data.getDisposalFormCode(),data.getAssetAuthNo(),data.getNum(),data.getNumUnit(),
            data.getPurApprovUnit(),data.getFixedAssAcqCode(),data.getDiposChannelCode(),data.getUseResponSubjectCode(),data.getAssetAuthSituCode(),
            data.getApprovStaCode(),data.getInitAssetVal(),data.getAccDep(),data.getNetVal(),data.getFundSource(),
            data.getPurOrgForm(),data.getDiposEarn(),data.getEarnSitu(),data.getAssetName(), data.getStartDate(),
            data.getEndDate(), data.getIsEnabled(), data.getValueTypeCode(),data.getUpdateTime(), data.getIsDeleted(),data.getCreateTime(),data.getProCode(),data.getIsFiscal(),data.getAgencyName()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public List<BasAssetBiz> getAssetFlowPage(String agencyCode, List<String> idList, PaginationDTO paginationDTO) {
        StringBuilder sql = new StringBuilder();

        sql.append("SELECT * FROM ").append(TABLE_NAME);
        sql. append(" WHERE 1=1 and ").append(com.boss.sys.utils.SysUtils.inSql("id", idList));
        if(!SysUtils.isNullOrEmpty(agencyCode)){
            sql.append(" and agency_code = '").append(agencyCode).append("'");
        }
        sql.append(" ORDER BY UPDATE_TIME DESC ");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(null, paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(BasAssetBiz.class), params);
    }

    @Override
    public int getAssetFlowTotal(List<String> idList) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT count(1) FROM ").append(TABLE_NAME);
        sql.append(" WHERE 1=1 and ").append(com.boss.sys.utils.SysUtils.inSql("id", idList));
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class);
    }

    @Override
    public void modifyAssetFlow(BasAssetBiz data) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME);
        sql.append(" set OFFICE_HOUSE_AREA=?, BISNISS_HOUSE_AREA=?, OTHER_HOUSE_AREA=?, LOC=?, " )
                .append("PRO_STA_DATE=?, PRO_CONS_STA=?, OWNERSHIP_CERT=?, LAND_CERTIFI_DATE=?, LAND_NO=?, ")
                .append("LAND_OWNERSHIP_AREA=?, STRUCTURE_AREA=?, USE_AREA=?, VEHICLE_TRAV_LICENCE_INFO=?, VEHICLE_OWNER=?, ")
                .append("VEHICLE_REGIS_DATE=?,  VEHICLE_IDENTI_CODE=?, VEHICLE_LICENCE_NO=?, VEHICLES_NUM=?, HOUSE_OWNERSHIP_CERT=?, ")
                .append("HOUSE_OWNER=?, HOUSE_OWNERSHIP_NO=?, HOUSE_OWNERSHIP_AREA=?, FLOOR_AREA=?, FI_LEADER=?, " )
                .append("SERVICE_LIFE=?, DEPRECIATION_METHOD=?, POSTER_DATE =?, ACCOUNTING_STATUS=?, VOUCHER_NO=?, IS_SHARED_PLATFORM=?, ")
                .append("IS_SHARING=?, VEHICLE_TYPE =?, VEHICLE_NUMB =?, VEHICLE_SPE =?, VEHICLE_LAW =?, ")
                .append("VEHICLE_MOD =?, INVESTED_AGENCY_INFO =?, INVESTED_AGENCY_STOCK_NO =?, IS_INVESTED_AGENCY_IN_MARKET =?, INVESTED_AGENCY_ORG_FORM_CODE =?, STOCK_PERC =?, ")
                .append("FINAL_INVEST_AMT =?, CUR_INVEST_AMT =?, PAR_VAL_AMT=?, TOTAL_INVES =?, ACC_INVES =?, " )
                .append("PRES_YEAR_INVES =?, STA_USE_DATE =?, NO_REG_ASSET_REASON =?, ACQU_DATE =?, INVESTED_AGENCY_CODE =?, ")
                .append("INVESTED_PRO_NAME =?, OFFICAL_VEH =?, ASSET_CODE=?, ")
                .append("FIXED_ASSET_TYPE_CODE=?, AGENCY_CODE=?, AGENCY_ID=?,FIXED_ASSET_STATE_CODE=?, OWNERSHIP_NATURE_CODE=?, ")
                .append("OWNERSHIP_FORM_CODE=?, DISPOSAL_FORM_CODE=?, ASSET_AUTH_NO=?, NUM=?, NUM_UNIT=?, " )
                .append("PUR_APPROV_UNIT=?, FIXED_ASS_ACQ_CODE=?, DIPOS_CHANNEL_CODE=?, USE_RESPON_SUBJECT_CODE=?, ASSET_AUTH_SITU_CODE=?, ")
                .append("APPROV_STA_CODE=?, INIT_ASSET_VAL=?, ACC_DEP=?,  NET_VAL=?, FUND_SOURCE =?, " )
                .append("PUR_ORG_FORM=?, DIPOS_EARN=?, EARN_SITU=?, ASSET_NAME=?, ")
                .append(" VALUE_TYPE_CODE=?, UPDATE_TIME=?,  PRO_CODE=?,IS_FISCAL=?, AGENCY_NAME=? where id=? ");

        Object[] params = new Object[]{
                 data.getOfficeHouseArea(), data.getBisnissHouseArea(), data.getOtherHouseArea(),data.getLoc(),
                data.getProStaDate(), data.getProConsSta(), data.getOwnershipCert(), data.getLandCertifiDate(), data.getLandNo(),
                data.getLandOwnershipArea(), data.getStructureArea(), data.getUseArea(), data.getVehicleTravLicenceInfo(),data.getVehicleOwner(),
                data.getVehicleRegisDate(), data.getVehicleIdentiCode(), data.getVehicleLicenceNo(),data.getVehiclesNum(),data.getHouseOwnershipCert(),
                data.getHouseOwner(), data.getHouseOwnershipNo(),data.getHouseOwnershipArea(),data.getFloorArea(), data.getFiLeader(),
                data.getServiceLife(), data.getDepreciationMethod(),data.getPosterDate(),data.getAccountingStatus(),data.getVoucherNo(), data.getIsSharedPlatform(),
                data.getIsSharing(),data.getVehicleType(),data.getVehicleNumb(),data.getVehicleSpe(), data.getVehicleLaw(),
                data.getVehicleMod(),data.getInvestedAgencyInfo(),data.getInvestedAgencyStockNo(),data.getIsInvestedAgencyInMarket(),data.getInvestedAgencyOrgFormCode(), data.getStockPerc(),
                data.getFinalInvestAmt(),data.getCurInvestAmt(), data.getParValAmt(),data.getTotalInves(), data.getAccInves(),
                data.getPresYearInves(),data.getStaUseDate(),data.getNoRegAssetReason(),data.getAcquDate(),data.getInvestedAgencyCode(),
                data.getInvestedProName(), data.getOfficalVeh(),data.getAssetCode(),
                data.getFixedAssetTypeCode(),data.getAgencyCode(),data.getAgencyId(),data.getFixedAssetStateCode(),data.getOwnershipNatureCode(),
                data.getOwnershipFormCode(),data.getDisposalFormCode(),data.getAssetAuthNo(),data.getNum(),data.getNumUnit(),
                data.getPurApprovUnit(),data.getFixedAssAcqCode(),data.getDiposChannelCode(),data.getUseResponSubjectCode(),data.getAssetAuthSituCode(),
                data.getApprovStaCode(),data.getInitAssetVal(),data.getAccDep(),data.getNetVal(),data.getFundSource(),
                data.getPurOrgForm(),data.getDiposEarn(),data.getEarnSitu(),data.getAssetName(),
                data.getValueTypeCode(),data.getUpdateTime(),data.getProCode(),data.getIsFiscal(),data.getAgencyName(),data.getId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public BasAssetBiz getAssetFlowById(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from " ).append(TABLE_NAME).append(" where id = ?");
        List<BasAssetBiz> basAssetBiz =  this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(BasAssetBiz.class), new Object[]{id});
        if(SysUtils.isNullOrEmpty(basAssetBiz)){
            return new BasAssetBiz();
        }
        return basAssetBiz.get(0);
    }

    @Override
    public void dropAssetFlowByIds(List<String> ids) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME).append(" set is_deleted='").append(IsDeletedEnum.DELETED.getValue())
                .append("' , is_enabled= '").append(IsEnabledEnum.UNENABLED.getValue())
                .append("' where ").append(com.boss.sys.utils.SysUtils.inSql("id", ids));
        this.getJdbcTemplate().update(sql.toString(), new Object[]{});

    }
}
